using Autofac;
using Autofac.Integration.Mvc;

using MyFileView.Core;
using MyFileView.Core.Repository;
using System;
using System.Linq;
using System.Reflection;
using System.Web.Caching;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MyFleView
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            try
            {
                var builder = new ContainerBuilder();
                var register = typeof(RegisterAutofac);
                var registerClass = typeof(RegisterClassAutofac);
                //builder.RegisterControllers(Assembly.GetCallingAssembly())//注册mvc的Controller
                //.PropertiesAutowired();//属性注入
                //  var builder = new ContainerBuilder();
                var assembly = AppDomain.CurrentDomain.GetAssemblies();
                //    var res=Assembly.GetExecutingAssembly();
               
                // var types = assembly.GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces().Contains(register)).ToArray();
                var types = assembly.SelectMany(x=>x.GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces().Contains(register))).ToArray();//;
                builder.RegisterTypes(types).AsImplementedInterfaces().InstancePerLifetimeScope();

                var types2 = assembly.SelectMany(x => x.GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces().Contains(registerClass))).ToArray();//;
                builder.RegisterTypes(types2).InstancePerLifetimeScope();

                var connectionString = WebConfigurationManager.AppSettings["Conn"];
                RegisterSqlSugar.AddSqlSugar(builder, connectionString);

                builder.RegisterControllers(typeof(MvcApplication).Assembly);//注册mvc的Controller
   // .PropertiesAutowired();//属性注入
                           //  builder.RegisterControllers(assembly);
                           // builder.RegisterTypes(types).AsImplementedInterfaces().InstancePerLifetimeScope();//
                DependencyResolver.SetResolver((AutoFacConfig.Register(builder)));//AutoFacConfig.Register(builder)
                
            }
            catch (Exception ex)
            {

                var x = ex;
                Console.WriteLine(ex);
            }

            //var container = builder.Build();




        }
    }
}
